package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.base.ResultMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.service.SiteService;
import com.symone.webcms.model.Site;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *   @description : 网站管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-03-19
 */
@RestController
@Api(value="/site", description="网站管理")
@RequestMapping("api/site")
public class SiteController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(SiteController.class);

    @Autowired
    public SiteService siteService;

    /**
     * @description : 获取网站分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
     */
    @RequiresPermissions("cms:site:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value="获取网站分页列表", notes="获取网站分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageSize",value="页大小",required=true,defaultValue = "10"),
            @ApiImplicitParam(name="pageNum",value="页数",required=true,defaultValue = "1")
    })
    public ResultMessage page(Integer pageSize,Integer pageNum) {
        Wrapper<Site> ew = new EntityWrapper<Site>();
        ew.where("deleted=0");

        //logger.debug("SQL:"+ew.originalSql());

        Page<Site> page = new Page<Site>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = siteService.findPage(page, ew);

        return ResultMessage.success("获取网站的分页列表",page.getRecords(),page.getTotal());
    }

    /**
     * @description : 获取所有Site记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
     */
    @RequiresPermissions("cms:site:index")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有网站信息", notes="获取所有网站信息")
    public ResultMessage list() {
        return ResultMessage.success("获取所有网站信息",siteService.findAll());
    }

    /**
     * @description : 通过id获取Site
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value="获取网站信息", notes="获取网站信息")
    @ApiImplicitParam(name="id",value="网站ID",required=true)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取网站信息成功!",siteService.selectById(id));
    }

    /**
     * @description : 添加Site
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value="添加网站信息", notes="添加网站信息")
    @ApiImplicitParam(name = "site", value = "网站信息", required = true )
    public ResultMessage add(Site site){
        siteService.insert(site);
        return ResultMessage.success("添加网站成功!");
    }

    /**
     * @description : 通过id逻辑删除Site
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
     */
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    @ApiOperation(value="逻辑删除网站", notes="逻辑删除网站")
    @ApiImplicitParam(name = "id", value = "网站Id", required = true )
    public ResultMessage removeFlag(Long id){
        if(siteService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑网站删除成功！");
        } else {
            return ResultMessage.fail("逻辑网站删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除网站
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
     */
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ApiOperation(value="物理删除网站", notes="物理删除网站")
    @ApiImplicitParam(name = "id", value = "网站Id", required = true )
    public ResultMessage remove(long id){
        if(siteService.deleteById(id)) {
            return ResultMessage.success("物理删除网站成功！");
        } else {
            return ResultMessage.fail("物理删除网站失败！");
        }
    }

    /**
     * @description : 更新Site信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value="更新网站信息", notes="更新网站信息")
    @ApiImplicitParam(name = "site", value = "网站信息", required = true )
    public ResultMessage update(Site site) {
        if(siteService.insertOrUpdate(site)) {
            return ResultMessage.success("网站信息更新成功!");
        }else{
            return ResultMessage.fail("网站信息更新失败!");
        }
    }

    /**
    * @description : 清除网站记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
    */
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    @ApiOperation(value="清除网站记录", notes="清除已被逻辑删除的网站记录")
    public ResultMessage clear(){
        int num = siteService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有网站记录被清除！");
        }
    }

}
